/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.section;

import java.util.Iterator;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.ObjectNode;
import org.eclipse.uml2.uml.ObjectNodeOrderingKind;

import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;

/**
 * Section for ordering of ObjectNodes
 *
 * @author Gilles Cannenterre
 * @since 11 avr. 07
 */
public class ObjectNodeOrderingSection extends PropertyViewSection {

	/** The combo type. */
	private CCombo comboObjectNodeOrderingKind;
	
	/** The combo type listener. */
	private SelectionListener comboObjectNodeOrderingKindListener;
	
	/* (non-Javadoc)
	 * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#createControls(org.eclipse.swt.widgets.Composite, org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage)
	 */
	@Override
	public void createControls(Composite parent, TabbedPropertySheetPage tabbedPropertySheetPage) {
		super.createControls(parent, tabbedPropertySheetPage);
		Composite composite = getWidgetFactory().createFlatFormComposite(parent);
		FormData data;
		getWidgetFactory().createCLabel(composite, "Ordering:"); //$NON-NLS-1$

		comboObjectNodeOrderingKind = getWidgetFactory().createCCombo(composite, SWT.NONE);
		data = new FormData();
		data.left = new FormAttachment(0, STANDARD_LABEL_WIDTH);
		data.top = new FormAttachment(0, 0);
		comboObjectNodeOrderingKind.setLayoutData(data);
		
		// Clean and fill the combo	
		comboObjectNodeOrderingKind.removeAll();
		// Add undefined element
		int comboIndex = 0;
		// Add the ObjectNodeOrderingKind values
		Iterator<ObjectNodeOrderingKind> objectNodeOrderingKindValues = ObjectNodeOrderingKind.VALUES.iterator();
		while(objectNodeOrderingKindValues.hasNext()) {
			ObjectNodeOrderingKind anObjectNodeOrderingKindValue = (ObjectNodeOrderingKind) objectNodeOrderingKindValues.next();
			comboObjectNodeOrderingKind.add(anObjectNodeOrderingKindValue.getName());
			comboObjectNodeOrderingKind.setData(anObjectNodeOrderingKindValue.getName(), anObjectNodeOrderingKindValue);		
			comboIndex = comboIndex + 1;
		}

		comboObjectNodeOrderingKindListener = new SelectionListener () {
			public void widgetSelected(SelectionEvent e) {
				ObjectNodeOrderingKind newObjectNodeOrderingKind = (ObjectNodeOrderingKind)(comboObjectNodeOrderingKind.getData(comboObjectNodeOrderingKind.getItem(comboObjectNodeOrderingKind.getSelectionIndex())));
				if (umlElement instanceof ObjectNode) {
					((ObjectNode)umlElement).setOrdering(newObjectNodeOrderingKind);
				}				
			}

			public void widgetDefaultSelected(SelectionEvent e) {}
		};
		
		comboObjectNodeOrderingKind.addSelectionListener(comboObjectNodeOrderingKindListener);		
	}

	/*
	 * @see org.eclipse.ui.views.properties.tabbed.view.ITabbedPropertySection#refresh()
	 */
	@Override
	public void refresh() {
		if(!comboObjectNodeOrderingKind.isDisposed()){
			if (umlElement instanceof ObjectNode) {
				// Get current parameter
				ObjectNodeOrderingKind currentObjectNodeOrderingKind = ((ObjectNode)umlElement).getOrdering();
				
				int comboIndex = 0;
				Iterator<ObjectNodeOrderingKind> objectNodeOrderingKindValues = ObjectNodeOrderingKind.VALUES.iterator();
				while(objectNodeOrderingKindValues.hasNext()) {
					ObjectNodeOrderingKind anObjectNodeOrderingKindValue = (ObjectNodeOrderingKind) objectNodeOrderingKindValues.next();
					// Select the current property type in the list
					if((currentObjectNodeOrderingKind != null) && currentObjectNodeOrderingKind.getName().equals(anObjectNodeOrderingKindValue.getName())) {
						comboObjectNodeOrderingKind.select(comboIndex);
					}
					comboIndex = comboIndex + 1;
				}
			}
		}

	}
	
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#dispose()
	 */
	@Override
	public void dispose(){
		super.dispose();
		if (comboObjectNodeOrderingKind!=null && !comboObjectNodeOrderingKind.isDisposed()) {
			comboObjectNodeOrderingKind.removeSelectionListener(comboObjectNodeOrderingKindListener);
		}		
	}
}